package com.vcaresolution.logistic.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.vcaresolution.logistic.form.JobregisterForm;
import com.vcaresolution.logistic.form.ShippingbillForm;
import com.vcaresolution.logistic.to.JobregisterTO;
import com.vcaresolution.logistic.to.ShippingbillTO;
import com.vcaresolution.logistic.util.ApplicationConstants;

/**
 * <code>ShippingbillController</code> is used to track all login related actions
 *
 * @author Dharam C. Gusai
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
public class ShippingbillController extends BaseController{

	@ModelAttribute(value=ApplicationConstants.SHIPPINGBILLFORM_MODELATTRIBUTE)
	public ShippingbillForm createForm(){
		return new ShippingbillForm();
	}
	
	private void loadRequiredDetailInModelObject(Integer jobId, ModelAndView modelAndView){
		//modelAndView.addObject(ApplicationConstants.JOBREGISTERLIST_MODELATTRIBUTE, getServiceRegistry().getJobRegisterService().findByAll());
		modelAndView.addObject(ApplicationConstants.PKGSTYPELIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("PKGSTYPE"));
		modelAndView.addObject(ApplicationConstants.CURRENCYLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("CURRENCY"));
		modelAndView.addObject(ApplicationConstants.INCOTERMSLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("INCOTERMS"));
		modelAndView.addObject(ApplicationConstants.WEIGHTTYPELIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("WEIGHTTYPE"));
		modelAndView.addObject(ApplicationConstants.CONTAINERSIZELIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("CONTAINERTYPE"));
		modelAndView.addObject(ApplicationConstants.DIRECTHUBLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("DIRECTHUB"));
		
		modelAndView.addObject(ApplicationConstants.PACKAGESTYPELIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().
																				  getCodetable("PKGSTYPE"));
		JobregisterTO jobRegisterTO = getServiceRegistry().getJobRegisterService().findById(jobId);
		
		ShippingbillForm shippingbillForm = new ShippingbillForm();
		shippingbillForm.setJobnoid(jobId);
		shippingbillForm.setCha(jobRegisterTO.getCha());
		
		modelAndView.addObject(ApplicationConstants.JOBREGISTERFORM_MODELATTRIBUTE, jobRegisterTO);
		modelAndView.addObject(ApplicationConstants.SHIPPINGBILLFORM_MODELATTRIBUTE, shippingbillForm);
	}
	
	/**
	 * This method is called when the GET request has been made to the ADDShippingbill Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_SHIPPINGBILL_DIRECT_URL,method = RequestMethod.GET)
	public ModelAndView loadAddShippingBillDirect() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(ApplicationConstants.ADD_SHIPPINGBILL_DIRECT_VIEW);
		modelAndView.addObject(ApplicationConstants.JOBREGISTERFORM_MODELATTRIBUTE, new JobregisterForm());
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ADDShippingbill Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_SHIPPINGBILL_URL,method = RequestMethod.GET)
	public ModelAndView loadAddShippingbill(@RequestParam(value="jobnoId") String jobId) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(ApplicationConstants.ADD_SHIPPINGBILL_VIEW);
		
		loadRequiredDetailInModelObject(Integer.parseInt(jobId), modelAndView);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADDShippingbill Request
	 */
	@RequestMapping(value=ApplicationConstants.LIST_SHIPPINGBILL_URL,method = RequestMethod.POST)
	public ModelAndView processAddShippingbill(@Valid ShippingbillForm shippingbillForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			shippingbillForm.setUser(getLoggedInUser(request));
			
			//Check for Exist ShippingBill Number And Container
			Boolean flag = getServiceRegistry().getShippingbillService().checkForExistShippingBillNumberAndContainer(shippingbillForm.getSbillno()
																		,shippingbillForm.getShippingcontainersForm().getContainerid());
			
			if(!flag){
				
				getServiceRegistry().getShippingbillService().add(shippingbillForm);
				modelAndView = list(""+shippingbillForm.getJobnoid());
			} else {
				
				modelAndView.addObject("message", getMessageSource().getMessage("shippingcontainersForm.exists.sbillNo", null, null));
				loadRequiredDetailInModelObject(shippingbillForm.getJobnoid(), modelAndView);
				
				List<ShippingbillTO> shippingbillList = getServiceRegistry().getShippingbillService().findByAll(shippingbillForm.getJobnoid());
				modelAndView.addObject(ApplicationConstants.SHIPPINGBILLLIST_MODELATTRIBUTE, shippingbillList);
				modelAndView.addObject(ApplicationConstants.SHIPPINGBILLFORM_MODELATTRIBUTE, shippingbillForm);
			}
			
		}else {			
			
			loadRequiredDetailInModelObject(shippingbillForm.getJobnoid(), modelAndView);
			
			List<ShippingbillTO> shippingbillList = getServiceRegistry().getShippingbillService().findByAll(shippingbillForm.getJobnoid());
			modelAndView.addObject(ApplicationConstants.SHIPPINGBILLLIST_MODELATTRIBUTE, shippingbillList);
			modelAndView.addObject(ApplicationConstants.SHIPPINGBILLFORM_MODELATTRIBUTE, shippingbillForm);
		}
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ListShippingbill Request
	 */
	@RequestMapping(value=ApplicationConstants.LIST_SHIPPINGBILL_URL,method = RequestMethod.GET)
	public ModelAndView list(@RequestParam(value="jobnoId") String jobId) {
		final ModelAndView modelAndView = loadAddShippingbill(jobId);
		
		List<ShippingbillTO> shippingbillList = getServiceRegistry().getShippingbillService().findByAll(Integer.parseInt(jobId));
		
		modelAndView.addObject(ApplicationConstants.SHIPPINGBILLLIST_MODELATTRIBUTE, shippingbillList);
		
		modelAndView.setViewName(ApplicationConstants.LIST_SHIPPINGBILL_VIEW);
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ADDShippingbill Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_SHIPPINGBILL_URL,method = RequestMethod.GET) 
	public  ModelAndView loadEditShippingbill(@RequestParam(value="jobnoId") Integer jobnoid, @RequestParam(value="sbId") String sbID
			, @RequestParam(value="shippingContainerId") String shippingContainerId) {
		
		ModelAndView modelAndView = new ModelAndView();
		
		//Getting Data of shippingbillObject for Edit
		ShippingbillTO shippingbillObject = getServiceRegistry().getShippingbillService().findById(Integer.parseInt(sbID),
																							Integer.parseInt(shippingContainerId));
		
		ShippingbillForm shippingbillForm = new ShippingbillForm();
		shippingbillForm.setJobnoid(jobnoid);
		
		BeanUtils.copyProperties(shippingbillObject, shippingbillForm);
		
		JobregisterTO jobRegisterTO = getServiceRegistry().getJobRegisterService().findById(shippingbillObject.getJobnoid());
		shippingbillForm.setCha(jobRegisterTO.getCha());
		
		loadRequiredDetailInModelObject(shippingbillObject.getJobnoid(), modelAndView);
		
		modelAndView.addObject(ApplicationConstants.JOBREGISTERFORM_MODELATTRIBUTE, jobRegisterTO);
		modelAndView.addObject(ApplicationConstants.SHIPPINGBILLFORM_MODELATTRIBUTE, shippingbillForm);
		
		List<ShippingbillTO> shippingbillList = getServiceRegistry().getShippingbillService().findByAll(shippingbillObject.getJobnoid());
		modelAndView.addObject(ApplicationConstants.SHIPPINGBILLLIST_MODELATTRIBUTE, shippingbillList);
		
		modelAndView.setViewName(ApplicationConstants.LIST_SHIPPINGBILL_VIEW);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADDShippingbill Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_SHIPPINGBILL_URL,method = RequestMethod.POST)
	public ModelAndView processEditShippingbill(@Valid ShippingbillForm shippingbillForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller:::::: :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			shippingbillForm.setUser(getLoggedInUser(request));
			
			getServiceRegistry().getShippingbillService().update(shippingbillForm);
			
			modelAndView = list(""+shippingbillForm.getJobnoid());
			
		}
		else {
			loadRequiredDetailInModelObject(shippingbillForm.getJobnoid(), modelAndView);
			
			List<ShippingbillTO> shippingbillList = getServiceRegistry().getShippingbillService().findByAll(shippingbillForm.getJobnoid());
			modelAndView.addObject(ApplicationConstants.SHIPPINGBILLLIST_MODELATTRIBUTE, shippingbillList);
			modelAndView.addObject(ApplicationConstants.SHIPPINGBILLFORM_MODELATTRIBUTE, shippingbillForm);
			modelAndView.setViewName(ApplicationConstants.LIST_SHIPPINGBILL_VIEW);
		}
		
		return modelAndView;
	}
	
}